home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / mmapfile / MMAPFILE.ZIP / UMEM2X.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-06-22  |  3.2 KB  |  136 lines

  1. unit UMem2X;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   E_MemMap, Buttons, StdCtrls, Mask, ComCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     SpeedButton1: TSpeedButton;
  12.     SpeedButton2: TSpeedButton;
  13.     Label1: TLabel;
  14.     NdxEdit: TMaskEdit;
  15.     SpeedButton3: TSpeedButton;
  16.     Label2: TLabel;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure SpeedButton1Click(Sender: TObject);
  20.     procedure SpeedButton2Click(Sender: TObject);
  21.     procedure SpeedButton3Click(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.     EMemMap : TEMemMap;
  25.   public
  26.     { Public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.DFM}
  35. Const
  36.       MaxArrSize = 16384;
  37. Type
  38.      IntArr = Array[0..MaxArrSize-1] Of Integer;
  39.      IntArrPtr = ^IntArr;
  40.  
  41. procedure TForm1.FormCreate(Sender: TObject);
  42. Var
  43.    I       : Integer;
  44.    IArr    : IntArrPtr;
  45. begin
  46.   Caption:='Int Array [0..'+IntToStr(MaxArrSize*2-1)+']';
  47.   EMemMap:=TEMemMap.Create(Self);
  48.   If NOT EMemMap.CreateMutex('DAVESMUTEX2') then
  49.     EMemMap.RaiseMappingException;
  50.   EMemMap.MemSize:=MaxArrSize*SizeOf(Integer)*2;
  51.   If NOT EMemMap.MapExisting('DAVESMAP2',SizeOf(IntArr)) then
  52.   begin
  53.     New(Iarr);
  54.     For I:=0 To MaxArrSize-1 do
  55.       IArr^[I]:=I+1;
  56.     Try
  57.       If NOT EMemMap.CreateMemMap('DAVESMAP2',SizeOf(IntArr),IArr^) then
  58.         EMemMap.RaiseMappingException;
  59.     Finally
  60.       Dispose(IArr);
  61.     end;
  62.     EMemMap.EnterCriticalSection;
  63.     If NOT EMemMap.Seek(1) then
  64.       EMemMap.RaiseMappingException;
  65.     For I:=0 To MaxArrSize-1 do
  66.       IntArrPtr(EMemMap.MemMap)^[I]:=MaxArrSize+I;
  67.     If NOT EMemMap.Seek(0) then
  68.       EMemMap.RaiseMappingException;
  69.     EMemMap.LeaveCriticalSection;
  70.   end;
  71. end;
  72.  
  73. procedure TForm1.FormDestroy(Sender: TObject);
  74. begin
  75.   EMemMap.Free;
  76. end;
  77. procedure TForm1.SpeedButton1Click(Sender: TObject);
  78. Var
  79.     AnInt : Integer;
  80. begin
  81.   AnInt:=StrToInt(NdxEdit.Text);
  82.   If (AnInt>=0) AND (AnInt<MaxArrSize) then
  83.   begin
  84.     EMemMap.EnterCriticalSection;
  85.     Try
  86.       AnInt:=IntArrPtr(EMemMap.MemMap)^[AnInt];
  87.     Finally
  88.       EMemMap.LeaveCriticalSection;
  89.     end;
  90.     MessageDlg(IntToStr(AnInt),mtInformation,[mbOk],0);
  91.   end;
  92. end;
  93.  
  94. procedure TForm1.SpeedButton2Click(Sender: TObject);
  95. Var
  96.     Ndx    : Integer;
  97.     AnInt  : Integer;
  98.     AValue : String;
  99. begin
  100.   Ndx:=StrToInt(NdxEdit.Text);
  101.   If (Ndx>=0) AND (Ndx<MaxArrSize) then
  102.   begin
  103.     EMemMap.EnterCriticalSection;
  104.     Try
  105.       AnInt:=IntArrPtr(EMemMap.MemMap)^[Ndx];
  106.       AValue:=IntToStr(AnInt);
  107.       InputQuery('Get Value','NewValue',AValue);
  108.       AnInt:=StrToInt(AValue);
  109.       IntArrPtr(EMemMap.MemMap)^[Ndx]:=AnInt;
  110.     Finally
  111.       EMemMap.LeaveCriticalSection;
  112.     end;
  113.   end;
  114. end;
  115.  
  116.  
  117. procedure TForm1.SpeedButton3Click(Sender: TObject);
  118. begin
  119.   If SpeedButton3.tag=0 then
  120.   begin
  121.     If NOT EMemMap.Seek(1) then
  122.       EMemMap.RaiseMappingException;
  123.     SpeedButton3.tag:=1;
  124.     SpeedButton3.Caption:='Map To 1st page';
  125.   end
  126.   else
  127.   begin
  128.     If NOT EMemMap.Seek(0) then
  129.       EMemMap.RaiseMappingException;
  130.     SpeedButton3.tag:=0;
  131.     SpeedButton3.Caption:='Map To 2nd page';
  132.   end;
  133. end;
  134.  
  135. end.
  136.